﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using cmsservice.DAL;
using cmsservice.Entity;
using System.IO;
using cmsservice.Common;

namespace cmsservice.BLL
{
    public class EMNewsInfoBLL
    {
        readonly EMNewsInfoDAL dal = new EMNewsInfoDAL();
        #region 查询EM_GRAB_QIANLONG_ZX表中的资讯新闻信息

        /// <summary>
        /// 获取所有的最新详细信息
        /// </summary>
        /// <returns></returns>
        public List<EMNewsInfoModel> GetAllNewsInfo(DateTime publDate)
        {
            List<EMNewsInfoModel> list = new List<EMNewsInfoModel>();
            DataTable dt = dal.GetAllNewsInfo(publDate);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                EMNewsInfoModel model = new EMNewsInfoModel();
                model.NewId = dt.Rows[i]["NewsId"].ToString();
                model.NewsTitle = dt.Rows[i]["NewsTitle"].ToString();
                model.ZXType = dt.Rows[i]["ZXType"].ToString();
                if (dt.Rows[i]["CreateDate"] != null)
                    model.CreateDate = Convert.ToDateTime(dt.Rows[i]["CreateDate"]).ToString("yyyy-MM-dd HH:mm:ss");
                if (dt.Rows[i]["PublDate"] != null)
                    model.PublDate = Convert.ToDateTime(dt.Rows[i]["PublDate"]).ToString("yyyy-MM-dd HH:mm:ss");
                model.NewsContent = dt.Rows[i]["NewsContent"].ToString();
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 个股资讯
        /// </summary>
        /// <param name="code">股票代码</param>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<EMNewsInfoModel> GetStockNewsInfo(string code)
        {
            List<EMNewsInfoModel> list = new List<EMNewsInfoModel>();
            DataTable dt = dal.GetAllNewsInfo(code);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                EMNewsInfoModel model = new EMNewsInfoModel();
                model.NewId = dt.Rows[i]["NewsId"].ToString();
                model.NewsTitle = dt.Rows[i]["NewsTitle"].ToString();
                model.ZXType = dt.Rows[i]["ZXType"].ToString();
                if (dt.Rows[i]["CreateDate"] != null)
                    model.CreateDate = Convert.ToDateTime(dt.Rows[i]["CreateDate"]).ToString("yyyy-MM-dd HH:mm:ss");
                if (dt.Rows[i]["PublDate"] != null)
                    model.PublDate = Convert.ToDateTime(dt.Rows[i]["PublDate"]).ToString("yyyy-MM-dd HH:mm:ss");
                model.NewsContent = dt.Rows[i]["NewsContent"].ToString();
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 获取所有有资讯的股票代码
        /// </summary>
        /// <returns></returns>
        public List<string> GetCodeList()
        {
            List<string> list = new List<string>();
            DataTable dt = dal.GetAllNewsCode();
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["code"] != null && dt.Rows[i]["code"].ToString() != "")
                        list.Add(dt.Rows[i]["code"].ToString());
                }
            }
            return list;
        }

        /// <summary>
        /// 简单数据转换
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<EMNewsInfoModel> GetList(DataTable dt)
        {
            List<EMNewsInfoModel> list = new List<EMNewsInfoModel>();
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    EMNewsInfoModel model = new EMNewsInfoModel();
                    if (dt.Rows[i]["NewsId"] != null && dt.Rows[i]["NewsId"].ToString() != "")
                    {
                        model.NewId = dt.Rows[i]["NewsId"].ToString();
                    }
                    if (dt.Rows[i]["CreateDate"] != null)
                        model.CreateDate = Convert.ToDateTime(dt.Rows[i]["CreateDate"]).ToString("yyyy-MM-dd HH:mm:ss");
                    model.NewsTitle = dt.Rows[i]["NewsTitle"].ToString();
                    if (dt.Rows[i]["PublDate"] != null)
                        model.PublDate = Convert.ToDateTime(dt.Rows[i]["PublDate"]).ToString("yyyy-MM-dd HH:mm:ss");
                    model.ZXType = dt.Rows[i]["ZXType"].ToString();

                    list.Add(model);
                }
            }
            return list;
        }

        /// <summary>
        /// 盯盘先锋》主力追踪》市场全景》资讯》每日股评 ZXType IN('大盘分析','基金看市')
        /// </summary>
        /// <returns></returns>
        public EMNewsInfoList GetEveryDayNews(string top)
        {
            EMNewsInfoList data = new EMNewsInfoList();
            object o = Cache.Get("GetEveryDayNews" + top);
            if (o == null)
            {
                //data.code = "0";
                data.url = "http://down3.emstock.com.cn/xg/mrgp/{newsid}.txt";
                data.NewsInfo = GetList(dal.GetEveryDayNews(top));
                if (data != null)
                    Cache.Add("GetEveryDayNews" + top, data, DateTime.Now.AddMinutes(5));
            }
            else
            {
                data = o as EMNewsInfoList;
            }
            return data;
        }

        /// <summary>
        /// 盯盘先锋》行情速递》分类排名》沪B股/深B股>行业资讯 ZXType IN('b股')
        /// </summary>
        /// <returns></returns>
        public EMNewsInfoList GetBGStockNews(string top)
        {
            EMNewsInfoList data = new EMNewsInfoList();
            object o =Cache.Get("GetBGStockNews" + top);
            if (o == null)
            {
                //data.code = "0";
                data.url = "http://down3.emstock.com.cn/xg/bgzx/{newsid}.txt";
                data.NewsInfo = GetList(dal.GetBGStockNews(top));
                if (data != null)
                    Cache.Add("GetBGStockNews" + top, data, DateTime.Now.AddMinutes(5));
            }
            else
            {
                data = o as EMNewsInfoList;
            }
            return data;
        }

        /// <summary>
        /// 盯盘先锋》基金页面/行情速递》分类排名》沪深基金》行业资讯 ZXType IN('基金动态'.'基金评述')
        /// </summary>
        /// <returns></returns>
        public EMNewsInfoList GetJJStockNews(string top)
        {
            EMNewsInfoList data = new EMNewsInfoList();
            object o =Cache.Get("GetJJStockNews" + top);
            if (o == null)
            {
                //data.code = "0";
                data.url = "http://down3.emstock.com.cn/xg/jj/{newsid}.txt";
                data.NewsInfo = GetList(dal.GetJJStockNews(top));
                if (data != null)
                    Cache.Add("GetJJStockNews" + top, data, DateTime.Now.AddMinutes(5));
            }
            else
            {
                data = o as EMNewsInfoList;
            }
            return data;
        }

        /// <summary>
        /// 盯盘先锋》行情速递》分类排名》H股/港股》行业资讯 ZXType IN('港股行情','港股')
        /// </summary>
        /// <returns></returns>
        public EMNewsInfoList GetGGStockNews(string top)
        {
            EMNewsInfoList data = new EMNewsInfoList();
            object o =Cache.Get("GetGGStockNews" + top);
            if (o == null)
            {
                //data.code = "0";
                data.url = "http://down3.emstock.com.cn/xg/gg/{newsid}.txt";
                data.NewsInfo = GetList(dal.GetGGStockNews(top));
                if (data != null)
                    Cache.Add("GetGGStockNews" + top, data, DateTime.Now.AddMinutes(5));
            }
            else
            {
                data = o as EMNewsInfoList;
            }
            return data;
        }

        /// <summary>
        /// 盯盘先锋》行情速递》股指期货》行业资讯 ZXType IN('股指期货')
        /// </summary>
        /// <returns></returns>
        public EMNewsInfoList GetGZQHStockNews(string top)
        {
            EMNewsInfoList data = new EMNewsInfoList();
            object o = Cache.Get("GetGZQHStockNews" + top);
            if (o == null)
            {
                //data.code = "0";
                data.url = "http://down3.emstock.com.cn/xg/gzqh/{newsid}.txt";
                data.NewsInfo = GetList(dal.GetGZQHStockNews(top));
                if (data != null)
                   Cache.Add("GetGZQHStockNews" + top, data, DateTime.Now.AddMinutes(5));
            }
            else
            {
                data = o as EMNewsInfoList;
            }
            return data;
        }

        /// <summary>
        /// 盯盘先锋》创业板（159915）》行业资讯、个股资讯 ZXType IN('创业板相关')
        /// </summary>
        /// <returns></returns>
        public EMNewsInfoList GetCYBStockNews(string top)
        {
            EMNewsInfoList data = new EMNewsInfoList();
            object o = Cache.Get("GetCYBStockNews" + top);
            if (o == null)
            {
                //data.code = "159915";
                data.url = "http://down3.emstock.com.cn/xg/cyb/{newsid}.txt";
                data.NewsInfo = GetList(dal.GetCYBStockNews(top));
                if (data != null)
                    Cache.Add("GetCYBStockNews" + top, data, DateTime.Now.AddMinutes(5));
            }
            else
            {
                data = o as EMNewsInfoList;
            }
            return data;
        }

        /// <summary>
        /// 盯盘先锋》中小板（159902）》行业资讯、个股资讯 ZXType IN('动态与评述')
        /// </summary>
        /// <returns></returns>
        public EMNewsInfoList GetDTPSStockNews(string top)
        {
            EMNewsInfoList data = new EMNewsInfoList();
            object o = Cache.Get("GetDTPSStockNews" + top);
            if (o == null)
            {
                //data.code = "159902";
                data.url = "http://down3.emstock.com.cn/xg/dtps/{newsid}.txt";
                data.NewsInfo = GetList(dal.GetDTPSStockNews(top));
                if (data != null)
                    Cache.Add("GetDTPSStockNews" + top, data, DateTime.Now.AddMinutes(5));
            }
            else
            {
                data = o as EMNewsInfoList;
            }
            return data;
        }

        /// <summary>
        /// 盯盘先锋》深100ETF（159901）、深成ETF（159903）、深红利（159905）、深成长（159906）、中小300（159907）、深F200（159908）、深TMT（159909）、深F120（159910）、民营ETF（159911）、深300ETF（159912）、深价值（159913）、深F60（159916）》行业资讯、个股资讯 ZXType IN('大盘分析','基金看市')
        /// </summary>
        /// <returns></returns>
        public EMNewsInfoList GetDPJJStockNews(string top)
        {
            EMNewsInfoList data = new EMNewsInfoList();
            object o = Cache.Get("GetDPJJStockNews" + top);
            if (o == null)
            {
                //data.code = "159901";
                data.url = "http://down3.emstock.com.cn/xg/mrgp/{newsid}.txt";
                data.NewsInfo = GetList(dal.GetDPJJStockNews(top));
                if (data != null)
                    Cache.Add("GetDPJJStockNews" + top, data, DateTime.Now.AddMinutes(5));
            }
            else
            {
                data = o as EMNewsInfoList;
            }
            return data;
        }

        /// <summary>
        /// 大盘分析
        /// </summary>
        /// <param name="top"></param>
        /// <returns></returns>
        public EMNewsInfoList GetDPFXStockNews(string top)
        {
            EMNewsInfoList data = new EMNewsInfoList();
            object o = Cache.Get("GetDPFXStockNews" + top);
            if (o == null)
            {
                //data.code = "159901";
                data.url = "http://down3.emstock.com.cn/xg/mrgp/{newsid}.txt";
                data.NewsInfo = GetList(dal.GetDPFXStockNews(top));
                if (data != null)
                    Cache.Add("GetDPFXStockNews" + top, data, DateTime.Now.AddMinutes(5));
            }
            else
            {
                data = o as EMNewsInfoList;
            }
            return data;
        }
        #endregion

        #region 获取环球股指，外汇，大宗商品
        /// <summary>
        /// 获取环球股指，外汇，大宗商品
        /// </summary>
        /// <returns></returns>
        public List<EMGZWHSPPriceModel> GetGZWHSPPrice()
        {
            List<EMGZWHSPPriceModel> list = new List<EMGZWHSPPriceModel>();
            object o = Cache.Get("GetGZWHSPPrice");
            if (o == null)
            {

                DataTable dt = dal.GetGZWHSPPrice();
                foreach (DataRow dr in dt.Rows)
                {
                    EMGZWHSPPriceModel model = new EMGZWHSPPriceModel();
                    model.name = dr["name"].ToString();
                    model.zdf = dr["zdf"].ToString();
                    model.zxz = dr["zxz"].ToString();
                    list.Add(model);
                }
                if (list != null)
                    Cache.Add("GetGZWHSPPrice", list, DateTime.Now.AddMinutes(60));
            }
            else
            {
                list = o as List<EMGZWHSPPriceModel>;
            }
            return list;
        }
        #endregion
    }
}
